@page "/{Slug?}"
@inherits AppComponentBase
@inject MarkdownPages Markdown
@inject AppConfig AppConfig
@using Markdig
@using Markdig.Syntax

@if (doc != null)
{
    <PageTitle>@doc.Title</PageTitle>
    
    <section class="mb-16 md:mb-12">
        <h1 class="text-4xl tracking-tight font-extrabold text-gray-900 dark:text-gray-50 sm:text-5xl md:text-6xl">
            @doc.Title
        </h1>
    </section>
    <div class="">
        <div class="prose lg:prose-xl mb-24">
            @BlazorHtml.Raw(doc.Preview)
        </div>
    </div>
}
else
{
    <div class="mt-3 mb-20 mx-auto max-w-fit">
        @if (error != null)
        {
            <ErrorSummary Status=@error />
        }
        else
        {
            <Loading />
        }
    </div>
}

@code {
    [Parameter]
    public required string Slug { get; set; }

    MarkdownFileInfo? doc;
    ResponseStatus? error;

    void load()
    {
        doc = Markdown.GetBySlug(Slug);
        if (doc == null)
        {
            error = new() { Message = $"_pages/{Slug}.md was not found" };
            return;
        }
    }

    protected override void OnInitialized() => load();

    protected override void OnParametersSet() => load();
}
